package com.sy.file;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

/**
 * 演示创建文件
 */
public class FileCreate {
    public static void main(String[] args) {

    }
    //方式1 new File(String pathname)
    @Test
    public void create01(){
        String filePath = "e:\\news1.txt";
        File file = new File(filePath);//现在file只是对象，只有执行力createNewFile()之后才会在磁盘创建该文件


        //通过ctrl+alt+t打开surroundWith选择try_catch
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    //方式2 new File(File parent,String child)//根据父目录文件+子路径构建
    //e:\\news2.txt
    @Test
    public void create02(){
        File parentFile = new File("e:\\");
        String fileName = "news2.txt";
        File file1 = new File(parentFile,fileName);//现在文件还只在内存中，没有写入磁盘

        try {
            file1.createNewFile();//将文件写入磁盘中
            System.out.println("文件创建成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //方式3 new File(String parent,String child)//根据父目录+子路径构建
    @Test
    public void create03(){
        String parentPath = "e:\\";
        String fileName = "news3";
        File file = new File(parentPath, fileName);

        try {
            file.createNewFile();
            System.out.println("文件创建成功~");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
